﻿using System;

using OctoOS.UI;
using OctoOS;

public partial class __admin_editCategory : OOSPage
{
	class PathInfo
	{
		[PathInfoMember(0, true)]
		[IntegerBase(64)]
		internal long CategoryID = 0;
	}

	PathInfo _pathInfo;

	protected void Page_Init(object sender, EventArgs e)
	{
		OOSRequest.Current.EnsureModerator();

		_pathInfo = EnablePathInfo<PathInfo>();
	}

    protected void Page_Load(object sender, EventArgs e)
    {
		AddBreadcrumb("", "/");
		AddBreadcrumb(Resources.Main.default_AdminPage, Urls.Admin.GetDefault());

		if(!IsPostBack)
		{
			if(_pathInfo.CategoryID == 0)
			{
				PageTitle = Resources.Main.editCategory_AddTitle;
			}
			else
			{
				var category = ApiService.GetCategory(OOSRequest.Current, _pathInfo.CategoryID);
				if(category == null)
					throw new ApplicationException();

				CategoryName.Text = category.Title;
				PageTitle = Resources.Main.editCategory_EditTitle;
			}
		}
    }

	protected void SubmitClick(object sender, EventArgs e)
	{
		if(IsValid)
		{
			if(_pathInfo.CategoryID == 0)
			{
				ApiService.InsertCategory(OOSRequest.Current, CategoryName.TrimmedText);
			}
			else
			{
				ApiService.UpdateCategory(OOSRequest.Current, _pathInfo.CategoryID,
					CategoryName.TrimmedText);
			}

			AppUtil.UpdateCategoriesList(OOSRequest.Current);
			Response.Redirect(Urls.Admin.GetDefault());
		}
	}
}